package learn.basic;

import java.util.Random;

import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.data.attributes.api.AttributeType;
import org.gephi.data.attributes.type.DynamicInteger;
import org.gephi.data.attributes.type.Interval;
import org.gephi.datalab.api.AttributeColumnsMergeStrategiesController;
import org.gephi.dynamic.api.DynamicController;
import org.gephi.dynamic.api.DynamicGraph;
import org.gephi.dynamic.api.DynamicModel;
import org.gephi.graph.api.Graph;
import org.gephi.graph.api.GraphController;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Node;
import org.gephi.io.generator.plugin.RandomGraph;
import org.gephi.io.importer.api.Container;
import org.gephi.io.importer.api.ContainerFactory;
import org.gephi.io.importer.api.ImportController;
import org.gephi.io.processor.plugin.DefaultProcessor;
import org.gephi.project.api.ProjectController;
import org.gephi.project.api.Workspace;
import org.openide.util.Lookup;

public class MetricsOverTime {

	public static void main(String[] args) {
		ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
		pc.newProject();
		Workspace workspace = pc.getCurrentWorkspace();
		
		Container container = Lookup.getDefault().lookup(ContainerFactory.class).newContainer();
		RandomGraph randomGraph = new RandomGraph();
		randomGraph.setNumberOfNodes(500);
		randomGraph.setWiringProbability(0.005);
		randomGraph.generate(container.getLoader());
		
		ImportController importController = Lookup.getDefault().lookup(ImportController.class);
		importController.process(container, new DefaultProcessor(), workspace);
		
		AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
		AttributeColumn dateColumn = attributeModel.getNodeTable().addColumn("date", AttributeType.INT);
		
		GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
		Graph graph = graphModel.getGraph();
		Random random = new Random();
		for (Node n : graph.getNodes()) {
			Integer randomDataValue = new Integer(random.nextInt(21) + 1990);
			n.getNodeData().getAttributes().setValue(dateColumn.getIndex(), randomDataValue);
		}
		
		AttributeColumnsMergeStrategiesController dataLabController = Lookup.getDefault().lookup(AttributeColumnsMergeStrategiesController.class);
		dataLabController.mergeNumericColumnsToTimeInterval(attributeModel.getNodeTable(), dateColumn, null, 1990, 2010);
		
		DynamicModel dynamicModel = Lookup.getDefault().lookup(DynamicController.class).getModel();
		DynamicGraph dynamicGraph = dynamicModel.createDynamicGraph(graph);
		
		DynamicInteger numberofNodes = new DynamicInteger();
		for (int i = 1990; i < 2009; i++) {
			int low = i;
			int high = i + 1;
			Graph subGraph = dynamicGraph.getSnapshotGraph(low, high);
			int count = subGraph.getNodeCount();
			numberofNodes = new DynamicInteger(numberofNodes, new Interval<Integer>(low, high, count));
		}
		
		System.out.println("Number of nodes: ");
		for (Interval<Integer> interval : numberofNodes.getIntervals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY)) {
			int low = (int) interval.getLow();
			int high = (int) interval.getHigh();
			System.out.println(low + "-" + high + " -> " + interval.getValue());
		}
	}

}